/*
 * @Author: your name
 * @Date: 2021-08-31 20:22:28
 * @LastEditTime: 2021-09-06 16:38:33
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \新建文件夹 (6)\broker\src\utils\axios.js
 */
import axios from 'axios';
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import {Toast} from 'vant'
axios.defaults.timeout = 100000;
const ajax = axios.create({
    timeout:'5000',
    
})
ajax.baseUrl='http://47.111.14.227:7001/api'
// 添加请求拦截器
axios.interceptors.request.use(function (config) {
    const token = sessionStorage.getItem('broker_token');
    if(token) config.headers.authorization = 'Bearer '+ token;
    // 进度条开启
    NProgress.start();
    // 在发送请求之前做些什么
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
    // 对响应数据做点什么
    // 进度条关闭
    NProgress.done();
    return response.data;
}, function (error) {
    // 判断状态码
    console.log(error);
    
    if(error.response.status === '401'){
        Toast.fail('未授权，无法登陆')
    }else if(error.response.status === '403'){
        Toast.fail('没有权限，禁止访问')
    }else if(error.response.status === '404'){
        Toast.fail('找不到页面，访问失败')
    }else if(error.response.status === '500'){
        Toast.fail('服务器内部错误')
    }
    // 对响应错误做点什么
    return Promise.reject(error);
});

export default axios;

